﻿
// bqDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "bq.h"
#include "bqDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CbqDlg 对话框



CbqDlg::CbqDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_BQ_DIALOG, pParent)
	, ZhanDouLi(FALSE)
	, ShangHai(FALSE)
	, ShengMing(FALSE)
	, TouFang(FALSE)
	, ShengMingHuiFu(FALSE)
	, ThreeBao(FALSE)
	, JiNengHuiSu(FALSE)
	, ShanBi(FALSE)
	, JinShangJianMian(FALSE)
	, YiDongSuDu(FALSE)
	, m_ZhanDouLi(0)
	, m_ShangHai(0)
	, m_ShengMing(0)
	, m_TouFang(0)
	, m_ShengMingHuiFu(0)
	, m_ThreeBao(0)
	, m_JiNengHuiSu(0)
	, m_ShanBi(0)
	, m_JinShangJianMian(0)
	, m_YiDongSuDu(0)
	, LoopNum(0)
	, m_AllQ(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
}

void CbqDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Check(pDX, IDC_CHECK2, ZhanDouLi);
	DDX_Check(pDX, IDC_CHECK4, ShangHai);
	DDX_Check(pDX, IDC_CHECK6, ShengMing);
	DDX_Check(pDX, IDC_CHECK8, TouFang);
	DDX_Check(pDX, IDC_CHECK10, ShengMingHuiFu);
	DDX_Check(pDX, IDC_CHECK3, ThreeBao);
	DDX_Check(pDX, IDC_CHECK5, JiNengHuiSu);
	DDX_Check(pDX, IDC_CHECK7, ShanBi);
	DDX_Check(pDX, IDC_CHECK9, JinShangJianMian);
	DDX_Check(pDX, IDC_CHECK11, YiDongSuDu);
	DDX_Text(pDX, IDC_EDIT2, m_ZhanDouLi);
	DDX_Text(pDX, IDC_EDIT3, m_ShangHai);
	DDX_Text(pDX, IDC_EDIT4, m_ShengMing);
	DDX_Text(pDX, IDC_EDIT5, m_TouFang);
	DDX_Text(pDX, IDC_EDIT6, m_ShengMingHuiFu);
	DDX_Text(pDX, IDC_EDIT7, m_ThreeBao);
	DDX_Text(pDX, IDC_EDIT8, m_JiNengHuiSu);
	DDX_Text(pDX, IDC_EDIT9, m_ShanBi);
	DDX_Text(pDX, IDC_EDIT10, m_JinShangJianMian);
	DDX_Text(pDX, IDC_EDIT11, m_YiDongSuDu);
	DDX_Text(pDX, IDC_EDIT1, LoopNum);
	DDX_Control(pDX, IDC_COMBO1, m_ComBo);
	DDX_Text(pDX, IDC_STATIC1, m_AllQ);
}

BEGIN_MESSAGE_MAP(CbqDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, &CbqDlg::OnBnClickedButton1)
	ON_CBN_SELCHANGE(IDC_COMBO1, &CbqDlg::OnCbnSelchangeCombo1)
	ON_EN_CHANGE(IDC_EDIT2, &CbqDlg::OnEnChangeEdit2)
	ON_EN_CHANGE(IDC_EDIT3, &CbqDlg::OnEnChangeEdit3)
	ON_EN_CHANGE(IDC_EDIT4, &CbqDlg::OnEnChangeEdit4)
	ON_EN_CHANGE(IDC_EDIT5, &CbqDlg::OnEnChangeEdit5)
	ON_EN_CHANGE(IDC_EDIT6, &CbqDlg::OnEnChangeEdit6)
	ON_EN_CHANGE(IDC_EDIT7, &CbqDlg::OnEnChangeEdit7)
	ON_EN_CHANGE(IDC_EDIT8, &CbqDlg::OnEnChangeEdit8)
	ON_EN_CHANGE(IDC_EDIT9, &CbqDlg::OnEnChangeEdit9)
	ON_EN_CHANGE(IDC_EDIT10, &CbqDlg::OnEnChangeEdit10)
	ON_EN_CHANGE(IDC_EDIT11, &CbqDlg::OnEnChangeEdit11)

	ON_BN_CLICKED(IDC_CHECK2, &CbqDlg::OnBnClickedCheck2)
	ON_BN_CLICKED(IDC_CHECK4, &CbqDlg::OnBnClickedCheck4)
	ON_BN_CLICKED(IDC_CHECK6, &CbqDlg::OnBnClickedCheck6)
	ON_BN_CLICKED(IDC_CHECK8, &CbqDlg::OnBnClickedCheck8)
	ON_BN_CLICKED(IDC_CHECK10, &CbqDlg::OnBnClickedCheck10)
	ON_BN_CLICKED(IDC_CHECK3, &CbqDlg::OnBnClickedCheck3)
	ON_BN_CLICKED(IDC_CHECK5, &CbqDlg::OnBnClickedCheck5)
	ON_BN_CLICKED(IDC_CHECK7, &CbqDlg::OnBnClickedCheck7)
	ON_BN_CLICKED(IDC_CHECK9, &CbqDlg::OnBnClickedCheck9)
	ON_BN_CLICKED(IDC_CHECK11, &CbqDlg::OnBnClickedCheck11)
END_MESSAGE_MAP()


// CbqDlg 消息处理程序
#include "SelfAdaptDPI.h"
BOOL CbqDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	m_ComBo.AddString(_T("冥王"));
	m_ComBo.AddString(_T("爆骷"));
	m_ComBo.AddString(_T("红雷"));
	m_ComBo.AddString(_T("橙雷"));

	m_ComBo.SetCurSel(0);
	LoopNum = 10000;
	ZhanDouLi = TRUE;
	ShangHai = TRUE;
	ShengMing = TRUE;
	TouFang = TRUE;
	ShengMingHuiFu = TRUE;
	ThreeBao = TRUE;
	JiNengHuiSu  = FALSE;
	ShanBi = TRUE;
	JinShangJianMian = TRUE;
	YiDongSuDu = FALSE;
	for (int i = IDC_CHECK2; i <= IDC_CHECK11; i++)
	{
		CWnd *pWnd = GetDlgItem(i);
		pWnd->EnableWindow(FALSE);

	}
	CWnd *pWnd = GetDlgItem(IDC_EDIT8);
	pWnd->EnableWindow(FALSE);
	pWnd = GetDlgItem(IDC_EDIT11);
	pWnd->EnableWindow(FALSE);

	m_AllQ = _T("总资质：");
	UpdateData(FALSE);

	CFont* ptf = this->GetFont();
	SelfAdaptDPI m_AdDPI(this);
	m_AdDPI();
	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CbqDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CbqDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CbqDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CbqDlg::UpdateAQ()
{
	UpdateData(TRUE);
	int AllQ = 0;
	int Q[] = { ZhanDouLi , ShangHai , ShengMing , TouFang , ShengMingHuiFu,
ThreeBao , JiNengHuiSu , ShanBi , JinShangJianMian , YiDongSuDu };
	int NowQ[] = { m_ZhanDouLi ,m_ShangHai ,m_ShengMing ,m_TouFang ,m_ShengMingHuiFu ,
m_ThreeBao ,m_JiNengHuiSu ,m_ShanBi ,m_JinShangJianMian ,m_YiDongSuDu };
	for (int i = 0; i < 10; i++)
	{
		AllQ += Q[i] * NowQ[i];
	}
	m_AllQ.Format(_T("总资质：%d"), AllQ);
	UpdateData(FALSE);
}

bool CbqDlg::UpdateEdit()
{
	UpdateData(TRUE);
	CWnd *pWnd = NULL;
	int Q[] = { ZhanDouLi , ShangHai , ShengMing , TouFang , ShengMingHuiFu,
ThreeBao , JiNengHuiSu , ShanBi , JinShangJianMian , YiDongSuDu };
	int num = 0;
	for (int i = 0; i < 10; i++)
	{
		num += Q[i];
		if (num > 7)
		{
			MessageBox(_T("超出资质上限"));
			return false;
		}
	}
	for (int i = IDC_EDIT2; i <= IDC_EDIT11; i++)
	{
		pWnd = GetDlgItem(i);
		pWnd->EnableWindow(Q[i - IDC_EDIT2]);
	}
	return true;
}

#include <time.h>
int UEvo[] = { 54,54,54,54,40,25,25,20,25,25 };
int MEvo[] = { 40,40,40,40,30,20,20,15,20,20 };
#define mEvoQ 220
#define mEvoL 240
#define uREvoQ 310
#define uOEvoQ 280
void CbqDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	int EvoL;
	int index = m_ComBo.GetCurSel();
	if (index == 3)
		EvoL = uOEvoQ;
	else
		EvoL = uREvoQ;

	srand((unsigned)time(NULL));
	int loop = LoopNum;
	int succ = 0;	
	int Q[] = { ZhanDouLi , ShangHai , ShengMing , TouFang , ShengMingHuiFu,
 ThreeBao , JiNengHuiSu , ShanBi , JinShangJianMian , YiDongSuDu };

	int IniMQ = 0, IniUQ = 0, AllQ = 0;
	for (int i = 0; i < 10; i++)
	{
		AllQ += Q[i];
		IniMQ += Q[i] * MEvo[i];
		IniUQ += Q[i] * UEvo[i];
	}
	if (index == 3 && AllQ != 7)
	{
		MessageBox(_T("该资质非橙雷资质！"));
		return;
	}
	int biasQ;
	if (index == 3)
		biasQ = mEvoQ - IniMQ;
	else
		biasQ = 0;

	if (IniMQ < mEvoQ)
	{
		CString str;
		if (IniUQ + biasQ < EvoL)
		{
			str.Format(_T("该宠物需要补%d~%d点资质才能进化！请确定已经补好资质"), biasQ, EvoL - IniUQ);
		}
		else
		{
			str.Format(_T("该宠物需要补%d点资质才能进化！请确定已经补好资质"), biasQ);
		}
		MessageBox(str);
	}
	
	while (loop--)
	{
		int NowQ[] = { m_ZhanDouLi ,m_ShangHai ,m_ShengMing ,m_TouFang ,m_ShengMingHuiFu ,
		m_ThreeBao ,m_JiNengHuiSu ,m_ShanBi ,m_JinShangJianMian ,m_YiDongSuDu };
		
		int randQ, randN;
		int AllQ = biasQ;
		int* EvoQL = MEvo;
		bool ExcQFlag = false;
		for (int i = 0; i < 10; i++)
		{
			AllQ += Q[i] * NowQ[i];
			if (NowQ[i] > EvoQL[i])
			{
				ExcQFlag = true;
				break;
			}
		}
		if (AllQ >= mEvoL || ExcQFlag)
		{
			EvoQL = UEvo;
		}
		
		while (AllQ < EvoL)
		{
			do
			{
				randQ = rand() % 10;

			} while (NowQ[randQ] == EvoQL[randQ]);

			if (randQ == 0 || randQ == 1 || randQ == 2 || randQ == 4 || randQ == 7) 
			{
				randN = rand() % 2;
			}
			else
			{
				randN = rand() % 4;
				if (randN == 3)
					randN = 1;
			}
			randN++;			

			if (NowQ[randQ] + randN <= EvoQL[randQ])
			{
				NowQ[randQ] += randN;
			}
			else
			{
				NowQ[randQ] = EvoQL[randQ];
			}

			AllQ = biasQ;
			for (int i = 0; i < 10; i++)
			{
				AllQ += Q[i] * NowQ[i];
			}

			if (AllQ - biasQ == IniUQ)
				break;

			if ((AllQ >= mEvoQ) && EvoQL != UEvo)
			{
				EvoQL = UEvo;
			}
		}
		if (NowQ[0] + NowQ[1] == 108)
		{
			succ++;
		}
	}
	CString str;
	str.Format(_T("496的概率为:%2.3f%%"), (float)succ / LoopNum * 100);
	MessageBox(str);
}


void CbqDlg::OnCbnSelchangeCombo1()
{
	// TODO: 在此添加控件通知处理程序代码
	int index = m_ComBo.GetCurSel();
	int* NowQ[] = { &m_ZhanDouLi ,&m_ShangHai ,&m_ShengMing ,&m_TouFang ,&m_ShengMingHuiFu ,
&m_ThreeBao ,&m_JiNengHuiSu ,&m_ShanBi ,&m_JinShangJianMian ,&m_YiDongSuDu };
	for (int i = 0; i < 10; i++)
	{
		*NowQ[i] = 0;
	}
	UpdateData(FALSE);
	UpdateAQ();
	CWnd *pWnd = NULL;
	switch (index)
	{
	case 0:
		ZhanDouLi = TRUE;
		ShangHai = TRUE;
		ShengMing = TRUE;
		TouFang = TRUE;
		ShengMingHuiFu = TRUE;
		ThreeBao = TRUE;
		JiNengHuiSu = FALSE;
		ShanBi = TRUE;
		JinShangJianMian = TRUE;
		YiDongSuDu = FALSE;
		for (int i = IDC_CHECK2; i <= IDC_CHECK11; i++)
		{
			pWnd = GetDlgItem(i);
			pWnd->EnableWindow(FALSE);

		}
		for (int i = IDC_EDIT2; i <= IDC_EDIT11; i++)
		{
			pWnd = GetDlgItem(i);
			pWnd->EnableWindow(TRUE);

		}
		pWnd = GetDlgItem(IDC_EDIT8);
		pWnd->EnableWindow(FALSE);
		pWnd = GetDlgItem(IDC_EDIT11);
		pWnd->EnableWindow(FALSE);
		break;
	case 1:
		ZhanDouLi = TRUE;
		ShangHai = TRUE;
		ShengMing = TRUE;
		TouFang = TRUE;
		ShengMingHuiFu = TRUE;
		ThreeBao = TRUE;
		JiNengHuiSu = TRUE;
		ShanBi = TRUE;
		JinShangJianMian = FALSE;
		YiDongSuDu = FALSE;
		for (int i = IDC_CHECK2; i <= IDC_CHECK11; i++)
		{
			pWnd = GetDlgItem(i);
			pWnd->EnableWindow(FALSE);
		}
		for (int i = IDC_EDIT2; i <= IDC_EDIT11; i++)
		{
			pWnd = GetDlgItem(i);
			pWnd->EnableWindow(TRUE);
		}
		pWnd = GetDlgItem(IDC_EDIT10);
		pWnd->EnableWindow(FALSE);
		pWnd = GetDlgItem(IDC_EDIT11);
		pWnd->EnableWindow(FALSE);
		break;
	case 2:
		ZhanDouLi = TRUE;
		ShangHai = TRUE;
		ShengMing = TRUE;
		TouFang = FALSE;
		ShengMingHuiFu = TRUE;
		ThreeBao = TRUE;
		JiNengHuiSu = TRUE;
		ShanBi = TRUE;
		JinShangJianMian = TRUE;
		YiDongSuDu = TRUE;
		for (int i = IDC_CHECK2; i <= IDC_CHECK11; i++)
		{
			pWnd = GetDlgItem(i);
			pWnd->EnableWindow(FALSE);
		}
		for (int i = IDC_EDIT2; i <= IDC_EDIT11; i++)
		{
			pWnd = GetDlgItem(i);
			pWnd->EnableWindow(TRUE);
		}
		pWnd = GetDlgItem(IDC_EDIT5);
		pWnd->EnableWindow(FALSE);
		break;
	case 3:
		ZhanDouLi = FALSE;
		ShangHai = FALSE;
		ShengMing = FALSE;
		TouFang = FALSE;
		ShengMingHuiFu = FALSE;
		ThreeBao = FALSE;
		JiNengHuiSu = FALSE;
		ShanBi = FALSE;
		JinShangJianMian = FALSE;
		YiDongSuDu = FALSE;
		for (int i = IDC_CHECK2; i <= IDC_CHECK11; i++)
		{
			pWnd = GetDlgItem(i);
			pWnd->EnableWindow(TRUE);
		}
		for (int i = IDC_EDIT2; i <= IDC_EDIT11; i++)
		{
			pWnd = GetDlgItem(i);
			pWnd->EnableWindow(FALSE);
		}
		break;
	default:
		break;
	}
	UpdateData(FALSE);
}



void CbqDlg::OnEnChangeEdit2()
{
	UpdateAQ();
}


void CbqDlg::OnEnChangeEdit3()
{
	UpdateAQ();
}

void CbqDlg::OnEnChangeEdit4()
{
	UpdateAQ();
}

void CbqDlg::OnEnChangeEdit5()
{
	UpdateAQ();
}

void CbqDlg::OnEnChangeEdit6()
{
	UpdateAQ();
}

void CbqDlg::OnEnChangeEdit7()
{
	UpdateAQ();
}

void CbqDlg::OnEnChangeEdit8()
{
	UpdateAQ();
}

void CbqDlg::OnEnChangeEdit9()
{
	UpdateAQ();
}

void CbqDlg::OnEnChangeEdit10()
{
	UpdateAQ();
}

void CbqDlg::OnEnChangeEdit11()
{
	UpdateAQ();
}


void CbqDlg::OnBnClickedCheck2()
{
	// TODO: 在此添加控件通知处理程序代码
	if (!UpdateEdit())
	{
		ZhanDouLi = false;
		UpdateData(FALSE);
	}
}


void CbqDlg::OnBnClickedCheck4()
{
	// TODO: 在此添加控件通知处理程序代码
	if (!UpdateEdit())
	{
		ShangHai = false;
		UpdateData(FALSE);
	}
}


void CbqDlg::OnBnClickedCheck6()
{
	// TODO: 在此添加控件通知处理程序代码
	if (!UpdateEdit())
	{
		ShengMing = false;
		UpdateData(FALSE);
	}
}


void CbqDlg::OnBnClickedCheck8()
{
	// TODO: 在此添加控件通知处理程序代码
	if (!UpdateEdit())
	{
		TouFang = false;
		UpdateData(FALSE);
	}
}


void CbqDlg::OnBnClickedCheck10()
{
	// TODO: 在此添加控件通知处理程序代码
	if (!UpdateEdit())
	{
		ShengMingHuiFu = false;
		UpdateData(FALSE);
	}
}


void CbqDlg::OnBnClickedCheck3()
{
	// TODO: 在此添加控件通知处理程序代码
	if (!UpdateEdit())
	{
		ThreeBao = false;
		UpdateData(FALSE);
	}
}


void CbqDlg::OnBnClickedCheck5()
{
	// TODO: 在此添加控件通知处理程序代码
	if (!UpdateEdit())
	{
		JiNengHuiSu = false;
		UpdateData(FALSE);
	}
}


void CbqDlg::OnBnClickedCheck7()
{
	// TODO: 在此添加控件通知处理程序代码
	if (!UpdateEdit())
	{
		ShanBi = false;
		UpdateData(FALSE);
	}
}


void CbqDlg::OnBnClickedCheck9()
{
	// TODO: 在此添加控件通知处理程序代码
	if (!UpdateEdit())
	{
		JinShangJianMian = false;
		UpdateData(FALSE);
	}
}


void CbqDlg::OnBnClickedCheck11()
{
	// TODO: 在此添加控件通知处理程序代码
	if (!UpdateEdit())
	{
		YiDongSuDu = false;
		UpdateData(FALSE);
	}
}
